/*
 * Assignment3part2.cpp
 *
 *  Created on: Mar 30, 2014
 *      Author: Anthony Varghese
 */


#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for the for_each function

using std::cout;
using std::endl;
using std::string;
using std::vector;

/*
 * This split method taks as input a string with 0 or more
 *    substrings (stretches of characters) separated by spaces
 *  The output of this function is a vector containing the substrings
 *    of the input string
 */
vector<string> split(string e){
	vector<string> result;

	string t;
	std::for_each( e.begin(),
				   e.end(),
				   // the next argument to the for_each is a C++11 lambda
				   [&](char c){
						if (isspace(c)){
							// note that the lambda function can access t and result
							if (t.length() != 0 )
								result.push_back( t );
							t = "";
						} else {
							t = t + c;
						}
					});
	// there can be one last one token:
	if (t.length() != 0 )
		result.push_back( t );
	return result;
}


int main(){
	string testExpression = "a1 + b - c * 45.3";
	cout << "Test expression is: " << testExpression << endl;

	/*
	 * Use the split function to break a string into its substrings that are separated by spaces
	 */
	vector<string> testTokens = split( testExpression );

	/*
	 * Print out the substrings
	 */
	for (const auto& t : testTokens){
		cout << "[" << t << "]";
	}
	cout << endl;
	cout << "Done!!" << endl;
}
